<!-- 
  @name: 选项卡
  @date: 2020.1.7
 -->
<template>
	<view class="tab-card" :style="{ width: tabWidth + 'rpx' }">
		<view
			class="__item"
			v-for="(item, index) in lists"
			:key="index"
			@tap="changeTab(item, index)">
			<view class="__text" :class="indexs === index ? 'std' : ''">
				<text>{{item}}</text>
				<view class="__line" :style="{ width: lineWidth }" v-show="indexs === index"></view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			tabList: {
				type: Array,
				default() {
					return []
				}
			},
			tabIndex: {
				type: [Number, String],
				default: 0
			},
			//tabcard宽度
			tabWidth: {
				type: [Number, String],
				default: 750
			},
			//__line长度
			lineWidth: {
				type: String,
				default: '84rpx'
			}
		},
		data() {
			return {
				lists: this.tabList,
				indexs: parseInt(this.tabIndex)  //选项卡索引
			}
		},
		watch: {
			tabList: {
				handler(newVal, oldVal) {
					this.lists = newVal
				},
				deep: true
			},
			tabIndex(newVal, oldVal) {
				if(newVal !== this.indexs) {
					this.indexs = newVal;
				}
			},
		},
		methods: {
			//点击选项卡
			changeTab(item, index) {
				if(this.indexs !== index) {
					this.indexs = index;
					this.$emit('clickItem', index);
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.tab-card {
		display: flex;
		flex-direction: row;
		background-color: $color-white;
		.__item {
			flex: 1;
			text-align: center;
			color: $color-grey;
		}
		.__text {
			position: relative;
			display: inline-block;
			height: $tab-card-heg;
			line-height: $tab-card-heg;
		}
		.__text.std {
			color: $color-text-primary;
		}
		.__line {
			position: absolute;
			left: 0;
			right: 0;
			bottom: 0;
			margin: auto;
			height: 8rpx;
			border-radius: 2rpx;
			background-color: $color-primary;
		}
	}
</style>